package io.renren.modules.ledger.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.ledger.dao.MemberDao;
import io.renren.modules.ledger.entity.MemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.ledger.dao.MemberConnDao;
import io.renren.modules.ledger.entity.MemberConnEntity;
import io.renren.modules.ledger.service.MemberConnService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberConnService")
public class MemberConnServiceImpl extends ServiceImpl<MemberConnDao, MemberConnEntity>
        implements MemberConnService {

    @Autowired
    private MemberConnDao dao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 单表分页查询
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<MemberConnEntity> page = this.selectPage(
                new Query<MemberConnEntity>(params).getPage(),
                new EntityWrapper<MemberConnEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 分页查询
     */
    @Override
    public PageUtils list4Page(Map<String, Object> params){
        Page<MemberConnEntity> page = new Query<MemberConnEntity>(params).getPage();
        return new PageUtils(page.setRecords(dao.list4Page(page, params)));
    }

    /**
     * 条件查询
     */
    @Override
    public List<MemberConnEntity> listByCondition(Map<String, Object> params) {
        return dao.listByCondition(params);
    }

    /**
     * 逻辑删除多条记录
     */
    @Override
    public Boolean deleteLogic(List ids) {
        return dao.deleteLogic(ids);
    }

    /**
     * 根据id查询详情
     */
    @Override
    public MemberConnEntity queryOneById(Long id) {
        return dao.queryOneById(id);
    }

    @Override
    @Transactional
    public R addConn(MemberConnEntity entity) {
        // 1 校验账号信息
        Map params = new HashMap();
        params.put("phone", entity.getConnPhone());
        params.put("status", 1);
        List<MemberEntity> list = memberDao.listByCondition(params);
        if(list == null || list.size() == 0){
            return R.error("不存在该账号，请重新输入");
        }
        entity.setConnId(list.get(0).getUserId());
        // 2 校验是否已经关联
        params = new HashMap();
        params.put("userId", entity.getUserId());
        params.put("connId", entity.getConnId());
        params.put("status", 1);
        List<MemberConnEntity> listConn = dao.listByCondition(params);
        if(listConn != null && listConn.size() > 0){
            return R.error("已经关联该用户");
        }
        this.insert(entity);
        // 2 同步数据并返回列表信息
        return this.sync(entity);
    }

    @Override
    public R sync(MemberConnEntity entity) {
        List<Map> connList = dao.sync(entity);
        return R.ok().put("connList", connList);
    }

}
